package org.ucas.kgraph.dto;

import org.ucas.kgraph.entity.Relation;
import org.ucas.kgraph.entity.RelationType;
import org.ucas.kgraph.utils.exception.NotFoundException;

/**
 * Relation的传输对象,请结合CarryDto和ListDto使用
 *
 * @author suemi
 */
public class RelationDto {

  public Long id;
  public Long leftend;
  public Long rightend;
  public String name;
  public Integer level;
  public String category;

  public static RelationDto of(Relation relation){
    if(relation==null) throw NotFoundException.Relation_NotExists;
    RelationDto dto = new RelationDto();
    dto.id = relation.getId();
    dto.leftend = relation.getLeftEnd();
    dto.rightend = relation.getRightEnd();
    dto.category = relation.getCategory().name;
    dto.name = relation.getName();
    dto.level = relation.getLevel();
    return dto;
  }

  /**
   * 更新Relation,但核心信息只允许relation中为空也就是创建时才可以写入
   * level等属性只允许计算得到,不允许手动写入
   * 其他属性只有非空时才会写入
   * @param relation
   * @return
   */
  public Relation writeTo(Relation relation){
    if(relation==null) throw NotFoundException.Relation_NotExists;

    if(relation.getId()==null) relation.setId(id);
    relation.setName(name);
    if(category!=null && relation.getCategory()==null)
      relation.setCategory(RelationType.forName(category));
    if(relation.getLeftEnd()==null) relation.setLeftEnd(leftend);
    if(relation.getRightEnd()==null) relation.setRightEnd(rightend);
    return relation;
  }


  public Long getLeft() {
    return leftend;
  }

  public void setLeft(Long left) {
    this.leftend = left;
  }

  public Long getRight() {
    return rightend;
  }

  public void setRight(Long right) {
    this.rightend = right;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getLevel() {
    return level;
  }

  public void setLevel(Integer level) {
    this.level = level;
  }

  public String getCategory() {
    return category;
  }

  public void setCategory(String category) {
    this.category = category;
  }
}
